package com.jasonzhou.onetomanycontrol.stack;

import android.app.Activity;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;


import com.jasonzhou.onetomanycontrol.base.AppContent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class ActivityStackManager extends SimpleActivityLifecycleCallbacks {
    private static final String TAG = "ActivityStackManager";
    private static final String BOUNDARY = "============================";
    private static ActivityStackManager sInstance = new ActivityStackManager();
    private int mCreateCount = 0;
    private int mStartCount = 0;
    private int mResumeCount = 0;
    private List<Activity> mActivities = new CopyOnWriteArrayList();
    private List<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks = new CopyOnWriteArrayList();
    private List<OnAppBackgroundListener> mOnAppBackgroundListeners;
    private static final byte[] LOCK = new byte[0];

    public static ActivityStackManager getInstance() {
        return sInstance;
    }

    private ActivityStackManager() {
    }

    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        super.onActivityCreated(activity, savedInstanceState);
        Log.d("ActivityStackManager", "============================ 进入 [" + activity.getClass().getSimpleName() + "@" + activity.hashCode() + "] " + DateTimeUtils.getCurDateTime() + "============================");
        ++this.mCreateCount;
        this.mActivities.add(activity);
        Iterator var3 = this.mActivityLifecycleCallbacks.iterator();

        while (var3.hasNext()) {
            ActivityLifecycleCallbacks callback = (ActivityLifecycleCallbacks) var3.next();
            callback.onActivityCreated(activity, savedInstanceState);
        }
        Log.e("TAG", activity.getClass().getSimpleName() + "启动");
    }


    public void onActivityStarted(Activity activity) {
        super.onActivityStarted(activity);
        ++this.mStartCount;
        Iterator var2 = this.mActivityLifecycleCallbacks.iterator();

        while (var2.hasNext()) {
            ActivityLifecycleCallbacks callback = (ActivityLifecycleCallbacks) var2.next();
            callback.onActivityStarted(activity);
        }

        if (this.mStartCount == 1) {
            this.notifyOnAppForeground();
        }

    }

    public void onActivityResumed(Activity activity) {
        super.onActivityResumed(activity);
        ++this.mResumeCount;
        Iterator var2 = this.mActivityLifecycleCallbacks.iterator();

        while (var2.hasNext()) {
            ActivityLifecycleCallbacks callback = (ActivityLifecycleCallbacks) var2.next();
            callback.onActivityResumed(activity);
        }

    }

    public void onActivityPaused(Activity activity) {
        super.onActivityPaused(activity);
        --this.mResumeCount;
        Iterator var2 = this.mActivityLifecycleCallbacks.iterator();

        while (var2.hasNext()) {
            ActivityLifecycleCallbacks callback = (ActivityLifecycleCallbacks) var2.next();
            callback.onActivityPaused(activity);
        }

    }

    public void onActivityStopped(Activity activity) {
        super.onActivityStopped(activity);
        --this.mStartCount;
        Iterator var2 = this.mActivityLifecycleCallbacks.iterator();

        while (var2.hasNext()) {
            ActivityLifecycleCallbacks callback = (ActivityLifecycleCallbacks) var2.next();
            callback.onActivityStopped(activity);
        }

        if (this.mResumeCount == 0) {
            this.notifyOnAppBackground();
        }

    }

    public void onActivityDestroyed(Activity activity) {
        super.onActivityDestroyed(activity);
        Log.d("ActivityStackManager", "============================ 退出 [" + activity.getClass().getSimpleName() + "@" + activity.hashCode() + "] " + DateTimeUtils.getCurDateTime() + "============================");
        this.mActivities.remove(activity);
        --this.mCreateCount;
        Iterator var2 = this.mActivityLifecycleCallbacks.iterator();

        while (var2.hasNext()) {
            ActivityLifecycleCallbacks callback = (ActivityLifecycleCallbacks) var2.next();
            callback.onActivityDestroyed(activity);
        }
        Log.e("TAG", activity.getClass().getSimpleName() + "销毁");
    }

    public int getStartCount() {
        return this.mStartCount;
    }

    public int getResumeCount() {
        return this.mResumeCount;
    }

    public Activity getTopActivity() {
        return !this.mActivities.isEmpty() ? (Activity) this.mActivities.get(this.mActivities.size() - 1) : null;
    }

    public void addActivityLifecycleCallbacks(ActivityLifecycleCallbacks callbacks) {
        if (!this.mActivityLifecycleCallbacks.contains(callbacks)) {
            this.mActivityLifecycleCallbacks.add(callbacks);
        }

    }

    public void removeActivityLifecycleCallbacks(ActivityLifecycleCallbacks callbacks) {
        if (this.mActivityLifecycleCallbacks != null) {
            this.mActivityLifecycleCallbacks.remove(callbacks);
        }

    }

    public void clearStack() {
        Iterator var1 = this.mActivities.iterator();

        while (var1.hasNext()) {
            Activity activity = (Activity) var1.next();
            activity.finish();
        }

        this.mActivities.clear();
    }

    public void finishAll(Context context) {
        Iterator var2 = this.mActivities.iterator();
        AppContent.instance.sccCommunication.disConnectDevice();
        while (var2.hasNext()) {
            Activity activity = (Activity) var2.next();
            activity.finish();
            var2.remove();
        }
        System.exit(0);
    }

    public void addOnAppBackgroundListener(OnAppBackgroundListener onAppBackgroundListener) {
        synchronized (LOCK) {
            if (this.mOnAppBackgroundListeners == null) {
                this.mOnAppBackgroundListeners = new ArrayList();
            }

            if (!this.mOnAppBackgroundListeners.contains(onAppBackgroundListener)) {
                this.mOnAppBackgroundListeners.add(onAppBackgroundListener);
            }

        }
    }

    public void removeOnAppBackgroundListener(OnAppBackgroundListener onAppBackgroundListener) {
        synchronized (LOCK) {
            if (this.mOnAppBackgroundListeners != null) {
                this.mOnAppBackgroundListeners.remove(onAppBackgroundListener);
            }

        }
    }

    public void notifyOnAppBackground() {
        synchronized (LOCK) {
            if (this.mOnAppBackgroundListeners != null && !this.mOnAppBackgroundListeners.isEmpty()) {
                Iterator var2 = this.mOnAppBackgroundListeners.iterator();

                while (var2.hasNext()) {
                    OnAppBackgroundListener onAppBackgroundListener = (OnAppBackgroundListener) var2.next();
                    onAppBackgroundListener.onBackground();
                }

            }
        }
    }

    public void notifyOnAppForeground() {
        synchronized (LOCK) {
            if (this.mOnAppBackgroundListeners != null && !this.mOnAppBackgroundListeners.isEmpty()) {
                Iterator var2 = this.mOnAppBackgroundListeners.iterator();

                while (var2.hasNext()) {
                    OnAppBackgroundListener onAppBackgroundListener = (OnAppBackgroundListener) var2.next();
                    onAppBackgroundListener.onForeground();
                }

            }
        }
    }

    public void finishActivity(@NonNull Class<? extends Activity> clz) {
        Iterator var2 = this.mActivities.iterator();

        while (var2.hasNext()) {
            Activity activity = (Activity) var2.next();
            if (activity.getClass().equals(clz)) {
                activity.finish();
            }
        }

    }

    public interface OnAppBackgroundListener {
        void onBackground();

        void onForeground();
    }
}

